package com.hdoit.apollo.web.shop.providers;

import java.util.ArrayList;
import java.util.List;

import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.viewmodel.shop.CustomerSearch;
import com.tennetcn.core.common.message.ListItem;
import com.tennetcn.core.common.utils.ChineseUtils;
import com.tennetcn.core.common.utils.CommonApplicationContextUtil;
import com.tennetcn.web.component.attribute.provider.ISingleOptionListProvider;

public class CustomerProvider implements ISingleOptionListProvider<ListItem>{
	
	ICustomerService customerService  = CommonApplicationContextUtil.getCurrentContext().getBean(ICustomerService.class);
	
	
	private List<ListItem> dataList;
	@Override
	public List<ListItem> getDataList(Object viewData) {
		CustomerSearch search = new CustomerSearch();
		List<Customer> customers = customerService.getList(search);
		if(customers==null||customers.size()<=0){
			return null;
		}
		dataList=new ArrayList<ListItem>();
		ListItem listItem=null;
		for (Customer customer : customers) {
			listItem=new ListItem();
			
	        String letter=ChineseUtils.getPinYinHeadChar(customer.getCustomerName()).toUpperCase();
			listItem.setText(customer.getCustomerName()+" "+letter);
			listItem.setValue(customer.getCustomerId());
			
			dataList.add(listItem);
		}
		return dataList;
	}

	@Override
	public ListItem getSelectedItem(Object value, Object viewData) {
		if(value==null){return null;}
		if(dataList==null){return null;}
		for (ListItem listItem : dataList) {
			if(listItem.getValue().equals(value)){
				return listItem;
			}
		}
		return null;
	}
}
